<template>
  <view class="search-container">
    <view class="search-box">
      <uni-search-bar
        cancelButton="none"
        :radius="100"
        @input="input"
      ></uni-search-bar>
    </view>
    <!-- 搜索结果 -->
    <view class="sugg-list" v-if="goodsList.length > 0">
      <view
        class="sugg-item"
        v-for="item in goodsList"
        :key="item.goods_id"
        @click="godetail(item.goods_id)"
      >
        <text class="goods-name">
          {{ item.goods_name }}
        </text>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" @click="clear" />
      </view>
      <!-- 历史主体 -->
      <view class="history-list">
        <uni-tag
          v-for="item in historyList"
          :key="item"
          :text="item"
          @click="goGoodsList(item)"
        />
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      historyList: [],
    };
  },
  onLoad() {
    this.historyList = uni.getStorageSync("history") || [];
  },
  methods: {
    input(e) {
      clearTimeout(this.timeid);
      this.timeid = setTimeout(() => {
        this.getGoodsList(e);
      }, 500);
    },
    async getGoodsList(query) {
      if (query.trim().length === 0) return;
      const res = await uni.$request({
        url: "goods/qsearch",
        data: {
          query,
        },
      });
      console.log(res);
      if (res.meta.status === 200) {
        this.goodsList = res.message;
      }
      this.saveHistory(query);
    },

    saveHistory(query) {
      const index = this.historyList.findIndex((item) => item === query);
      if (index > -1) {
        this.historyList.splice(index, 1);
      }
      this.historyList.unshift(query);
      uni.setStorageSync("history", this.historyList);
    },
    clear() {
      console.log("111");
      this.historyList = [];
      uni.setStorageSync("history", []);
    },
    godetail(goods_id) {
      console.log("111");
      uni.navigateTo({
        url: "/subpack/goods-detail/index?goods_id=" + goods_id,
      });
    },
    goGoodsList(query) {
      uni.navigateTo({ url: "/subpack/goods-list/index?query=" + query });
    },
  },
};
</script>


<style lang="scss" scoped>
.search-container {
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
    ::v-deep .uni-searchbar {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: row;
      position: relative;
      padding: 16rpx;
      /* 将默认的 #FFFFFF 改为 #C00000 */
      background-color: #c00000;
    }
  }
  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      ::v-deep .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
}
</style>